﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Bestcomy.Web.Controls.Upload;
using System.IO;

public partial class test_upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["sUserName"] = "dyuanming";
        Label1.Visible = false;
        //if (!IsPostBack)
        //{
            Session["sTittle"] = "批量建立用户";
            //获取上传文件保存的路径
            string fpath = Path.Combine(Request.PhysicalApplicationPath, "temp");
            if (!Directory.Exists(fpath))
            {
                Directory.CreateDirectory(fpath);
            }
            AspnetUpload upldr = new AspnetUpload();
            upldr.set_UploadFolder(fpath);
            upldr.RegisterModelessProgressBar(this.Button1);

        //}
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //测试文件类型是否符合的变量
        Boolean fileOK = false;
        //设置服务器中保存文件的路径
        String path = Server.MapPath("~/temp/");
        //判断是否选择了文件
        if (FileUpload1.HasFile)
        {
            //返回文件的扩展名
            String fileExtension =
                System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            //设置限定的文件类型
            String[] allowedExtensions =
                //{ ".txt", ".doc", ".wmv", "asx",".jpg","xml" ,""swf"};
            { ".txt" };
            //判断用户选择的文件类型是否受限
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }
        //如果文件大于1M，则不允许上传
        if (FileUpload1.PostedFile.ContentLength > 10240000)
        {
            fileOK = false;
        }
        //如果文件类型符合
        if (fileOK)
        {
            try
            {
                // 将文件保存到指定的文件夹下
                //string zfilename = Session["sUserName"].ToString()+".txt";
                //string zfilename = Session["sUserName"].ToString() + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                string zfilename = Session["sUserName"].ToString() + ".txt";
                FileUpload1.PostedFile.SaveAs(path + zfilename);
                Label1.Visible = true;
                Label1.Text = "文件上传成功！";
                //Button2.Enabled = true;
            }
            catch
            {
                Label1.Visible = true;
                Label1.Text = "无法实现文件的上传。";
            }
        }
        else
        {
            Label1.Visible = true;
            Label1.Text = "本次上传文件未成功。文件类型不对或文件超出10M。";
        }
    }


}
